// 演示单线程和多线程效率差别
public class ThreadTest {
    public static void main(String[] args) {
        serial();
        concurrency();
    }

    public static void serial(){
        long begin = System.currentTimeMillis();

        long a = 0;
        for (long i = 0; i < 100_0000_0000L; i++) {  // int表示数据范围：-21亿~21亿，超过范围用long,且数值后加上L
            a++;
        }
        long b = 0;
        for (long i = 0; i < 100_0000_0000L; i++) {
            b++;
        }

        long end = System.currentTimeMillis();
        System.out.println("一个线程执行："+(end-begin)+"ms");
    }

    public static void concurrency(){  //concurrency:并发的

        Thread t1 = new Thread(()->{
            long a = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {
                a++;
            }
        });

        Thread t2 = new Thread(()->{
            long b = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {  // int表示数据范围：-21亿~21亿，超过范围用long,且数值后加上L
                b++;
            }
        });
        long begin = System.currentTimeMillis();
        t1.start();
        t2.start();

        // 确保t1和t2已经执行完
        try{
            t1.join();
            t2.join();
        }catch (Exception e){
            e.printStackTrace();
        }

        // 这时main从阻塞中恢复，继续执行下面代码：
        long end = System.currentTimeMillis();
        System.out.println("两个线程并发执行："+(end-begin)+"ms");
    }
}
